const mongoose = require('mongoose');
const Schema = mongoose.Schema
const {Mixed,ObjectId} = Schema.Types;  //  可以存储任何类型的数据

//  定义movie数据类型的Schema
const MovieSchema = new Schema({
    category:{
        type:ObjectId,
        ref:'Category'
    },
    doubanId:String,
    rate:Number,
    title:String,
    summary:String,
    video:String,

    videoKey:String,
    posterKey:String,
    coverKey:String,

    poster:String,
    cover:String,
    rawTitle:String,
    movieTypes:[String],
    pubdate:Mixed,
    year:Number,
    tags:[String],
    meta:{
        createAt:{
            type:Date,
            default:Date.now()
        },
        updatedAt:{
            type:Date,
            default:Date.now()
        }
    }
})

//  在保存数据save之前进行对时间的操作
MovieSchema.pre('save',next=>{
    if(this.isNew){
        this.meta.createAt = this.meta.updatedAt = Date.now()
    }else{
        this.meta.updatedAt = Date.now()
    }
    next()
})

const MovieModel = mongoose.model('Movie',MovieSchema)

module.exports =  MovieModel